<?php

/**
 * BuscaController - Pesquisa
 *
 * @author Fernando Dias Oliveira <rodox17@gmail.com>
 * @package intranet
 * @subpackage controller
 */

class BuscaController extends AppBaseController
{
	private $table;
	private $select;
	private $grid;
	private $tabelaF;
	
	public function init()
	{
		header("Content-Type: text/html; charset=ISO-8859-1", true);
	}

    public function indexAction() 
    {
    	$table    = $this->_request->getParam("table");
    	$busca    = $this->_request->getPost("busca");
    	$parans   = $this->_request->getPost("parans");
    	$condicao = $this->_request->getPost("condicao");
    	$order    = $this->_request->getPost("order");
    	$usuario  = $this->_request->getPost("usuario");
    	$like     = $this->_request->getPost("like");
    	
		$this->grid = new Grid($table);
		
		$this->tabelaF = $table;
		
        if($usuario)
    	{
    		array_push($this->grid->fields, "login");
    	}
    	
    	$this->grid->select();
    	
		foreach ($busca as $key => $val) 
		{
			if(!$busca[$key])
			{
				continue;
			}
		
			$str = utf8_decode($busca[$key]);
			
			if($like)
			{
				$this->grid->select->orWhere("$parans[$key] LIKE '%$str%'");
			}
			else
			{
				$this->grid->select->orWhere("$parans[$key] $condicao[$key] '$str'");				
			}
		}
		
		if($order)
		{
			//$this->grid->select->order($order);
		}
		
		$this->especific();
    }
    
    public function run()
    {
		$this->view->fields    = $this->grid->run();
		$this->view->head      = $this->grid->fields;
		$this->view->primary   = $this->grid->primary;
		$this->view->status    = $this->grid->status;
		$this->view->paginacao = $this->grid->paginacao;
		
		/**
		 * Tabela de busca.
		 */
		
		$this->view->table = $this->tabelaF;
		
		$this->_helper->layout->disableLayout();
    }
    
    public function especific()
    {
    	$controller = $this->_request->getParam("control");
    	
    	if(method_exists($this, $controller))
    	{
    		$this->$controller();
    	}
    	
    	$this->run();
    }

    public function where($data)
    {
    	foreach ($data as $key => $val)
		{
			if(!$val)
			{
				continue;
			}
			
			$str = utf8_decode($busca[$key]);
			
			$this->grid->select->where("$key = '$str'");
		}
    }
    
    public function documentos()
    {
    	extract ($_POST, EXTR_OVERWRITE);
    	
    	$data = array(
    		"tipodocumento" => $tipodocumento,
    		"origem"                         => $origem,
    		"destino"                        => $destino,
    		"assunto"                        => $assunto,
    		"status"                         => $status,
    		Data::getDataBanco("datainicio") => $datainicio,
    		Data::getDataBanco("datasaida")  => $datafinal
    	);
    	
        if(!$doc_status)
    	{
    		//$data["ativo"] = 2;
    	}
    	
		$this->where($data);
   	}
   	
   	public function autocompleteAction()
   	{
   		$tabela = $this->_request->getParam("tabela");
   		$field  = $this->_request->getParam("field");
   		
   		$obj = new $tabela;
   		
   		if(method_exists($obj, "getView"))
   		{
   			$tabela = $obj->getView();
   		}
   		
   		$db = $obj->getAdapter();
   		
   		$sql = $db->select()->from($tabela, array($field))->__toString();
   		
   		foreach($db->fetchAll($sql) as $key => $val)
   		{
   			if($val)
   			{
   				$var .= "{$val[$field]}|\n";
   			}
   		}
   		
   		
   		echo $var;
   		
   		//echo "<br/>teste|\nbla|";

   		exit;
   	}
}

?>